package com.gitee.huanminabc.utils_test.dal.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.huanminabc.dynamicdb.aop.DBTransactional;
import com.gitee.huanminabc.utils_test.dal.mapper.TUserDatasourceMapper;
import com.gitee.huanminabc.utils_test.entity.TUserDatasourceEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Author mac
 * @Date 2024-12-06 09:57:15
 */
@Service
public class TUserDatasourceManager extends ServiceImpl<TUserDatasourceMapper, TUserDatasourceEntity> implements IService<TUserDatasourceEntity> {

    @Autowired
    private TUserDatasourceMapper tUserDatasource;

    /**
     * 分页查询
     * @param entity
     * @param pageNo
     * @param pageSize
     * @return
     */
    public   IPage<TUserDatasourceEntity> page(TUserDatasourceEntity entity, long pageNo, long pageSize) {
       return page(entity,pageNo,pageSize,true);
    }
    /**
     * 分页查询 不统计总数,一般用于导出数据的时候使用
     * @param entity
     * @param pageNo
     * @param pageSize
     * @return
     */
    public   IPage<TUserDatasourceEntity> pageNotCount(TUserDatasourceEntity entity, long pageNo, long pageSize) {
         return page(entity,pageNo,pageSize,false);
    }
    
    private IPage<TUserDatasourceEntity> page(TUserDatasourceEntity entity, long pageNo, long pageSize,boolean isSearchCount) {
        Page<TUserDatasourceEntity> page = new Page<>(pageNo, pageSize,isSearchCount);
        LambdaQueryWrapper<TUserDatasourceEntity> wrapper = getWrapperQuery(entity);
        return tUserDatasource.selectPage(page, wrapper);
    }


    /**
      * 条件查询
      * @param entity
      * @return
      */
    public List<TUserDatasourceEntity> query(TUserDatasourceEntity entity) {
        LambdaQueryWrapper<TUserDatasourceEntity> wrapper = getWrapperQuery(entity);
        return tUserDatasource.selectList( wrapper);
    }


    /**
      * 查询条件
      * @param entity
      * @return
      */
    public LambdaQueryWrapper<TUserDatasourceEntity> getWrapperQuery(TUserDatasourceEntity entity) {
        return new QueryWrapper<TUserDatasourceEntity>().lambda()
				.eq(Objects.nonNull(entity.getId()), TUserDatasourceEntity::getId, entity.getId())
				.eq(StringUtils.isNotEmpty(entity.getIp()), TUserDatasourceEntity::getIp, entity.getIp())
				.eq(Objects.nonNull(entity.getPort()), TUserDatasourceEntity::getPort, entity.getPort())
				.eq(StringUtils.isNotEmpty(entity.getDbName()), TUserDatasourceEntity::getDbName, entity.getDbName())
				.eq(StringUtils.isNotEmpty(entity.getUserName()), TUserDatasourceEntity::getUserName, entity.getUserName())
				.eq(StringUtils.isNotEmpty(entity.getPassWord()), TUserDatasourceEntity::getPassWord, entity.getPassWord())
				.eq(StringUtils.isNotEmpty(entity.getAccount()), TUserDatasourceEntity::getAccount, entity.getAccount())
				.eq(StringUtils.isNotEmpty(entity.getUpdateBy()), TUserDatasourceEntity::getUpdateBy, entity.getUpdateBy())
				.eq(Objects.nonNull(entity.getVersion()), TUserDatasourceEntity::getVersion, entity.getVersion())
				.eq(Objects.nonNull(entity.getState()), TUserDatasourceEntity::getState, entity.getState())
				.eq(StringUtils.isNotEmpty(entity.getType()), TUserDatasourceEntity::getType, entity.getType())
				.orderByDesc(TUserDatasourceEntity::getCreateTime);
    }

    //事务
//    @Transactional(rollbackFor = Exception.class)
    @DBTransactional
    public void updateByTran() {
        TUserDatasourceEntity one = getOne(new QueryWrapper<TUserDatasourceEntity>().lambda().eq(TUserDatasourceEntity::getId, 1782278468064092169L));
        one.setUpdateBy("test1111");
        updateById(one);
        one.setUpdateBy("test222");
        updateById(one);

    }

    @DBTransactional()
    public void updateByTran2() {
        TUserDatasourceEntity one = getOne(new QueryWrapper<TUserDatasourceEntity>().lambda().eq(TUserDatasourceEntity::getId, 1782278468064092169L));
        one.setUpdateBy("test111");
        updateById(one);
        int a=1/0;
        one.setUpdateBy("test222");
        updateById(one);
    }

}